from typing import List, Dict, Any, Optional, Union
from pydantic import BaseModel, Field
from datetime import datetime


class ResponseBase(BaseModel):
    """基础响应模型"""
    code: int = 200
    message: str = "success"


class SchoolBase(BaseModel):
    """学校基础模型"""
    name: str
    forum_link: Optional[str] = None
    forum_name: Optional[str] = None


class SchoolCreate(SchoolBase):
    """创建学校请求模型"""
    pass


class SchoolResponse(SchoolBase):
    """学校响应模型"""
    id: int
    # 暂时不需要
    # created_at: Optional[datetime] = None

    class Config:
        from_attributes = True


class SchoolListResponse(ResponseBase):
    """学校列表响应模型"""
    data: List[SchoolResponse]


class ClassBase(BaseModel):
    """班级基础模型"""
    class_code: str
    class_name: str
    start_date: str = "2025-09-01"
    college_name: str
    grade_name: str
    major_code: str
    major_name: str
    major_direction: Optional[str] = None
    education_level: str
    student_count: Optional[str] = "0"
    academic_year_code: str
    academic_year_name: str
    term_code: str
    campus_id: str
    campus_name: str


class ClassCreate(ClassBase):
    """创建班级请求模型"""
    school_id: int


class ClassResponse(ClassBase):
    """班级响应模型"""
    id: int
    school_id: int
    created_at: Optional[datetime] = None

    class Config:
        from_attributes = True


class ClassDetailResponse(ResponseBase):
    """班级详情响应模型"""
    data: ClassResponse


class ClassAllListResponse(ResponseBase):
    """班级全量列表响应模型（管理员）"""
    data: List[ClassResponse]


class ClassInfo(BaseModel):
    """班级信息模型"""
    class_code: str
    class_name: str
    start_date: str
    college_name: str


class FilterOption(BaseModel):
    """筛选选项模型（新版）"""
    value: str
    text: str  # 显示文本
    count: int


class ClassFilters(BaseModel):
    """班级筛选条件模型（旧版，保留兼容性）"""
    academic_years: List[str]
    terms: List[str]
    campuses: List[str]
    colleges: List[str]
    grades: List[str]
    majors: List[str]
    education_levels: List[str]


class FilterDimension(BaseModel):
    """筛选维度模型"""
    field: str  # 字段名
    title: str  # 中文标题
    options: List[FilterOption]  # 可选项列表


class ClassSimple(BaseModel):
    """简化班级信息"""
    class_name: str
    class_code: str


class ClassCoursesRequest(BaseModel):
    """获取班级课程请求模型"""
    class_code: str = Field(..., description="班级编号（必填）")
    school_id: Optional[int] = Field(None, description="学校ID（可选，用于提高查询稳定性）")


class DynamicClassFilters(BaseModel):
    """动态班级筛选条件模型（支持联动筛选）"""
    filters: List[FilterDimension]  # 筛选维度列表
    classes_data: List[ClassSimple]  # 可选班级列表
    total_classes: int
    applied_filters: Dict[str, Any]


class ClassFiltersResponse(ResponseBase):
    """班级筛选条件响应模型"""
    data: DynamicClassFilters


class ClassListResponse(ResponseBase):
    """班级列表响应模型"""
    data: Dict[str, Any]


class TeacherInfo(BaseModel):
    """教师信息模型"""
    name: str
    title: Optional[str] = None
    role: Optional[str] = None


class SectionInfo(BaseModel):
    """课程节次信息模型"""
    start: int
    end: int


class WeeksInfo(BaseModel):
    """课程周次信息模型"""
    start: int
    end: int
    pattern: str  # every, odd, even


class CourseBase(BaseModel):
    """课程基础模型"""
    course_name: str
    course_name_abbr: Optional[str] = None
    teacher: TeacherInfo
    day_of_week: int
    dateDigitSeparator: str
    campus: str
    location: Dict[str, Any]
    section: SectionInfo
    weeks: WeeksInfo
    className: str
    all_classes: List[str]
    courseType: Optional[str] = None
    credit: Optional[str] = None
    classHour: Optional[str] = None
    studentCount: Optional[str] = "0"
    assessment: Optional[str] = None
    teachingMethod: Optional[str] = None
    bremark: Optional[Dict[str, Any]] = None


class CourseCreate(CourseBase):
    """创建课程请求模型"""
    class_id: int
    class_code: str


class CourseResponse(CourseBase):
    """课程响应模型"""
    id: int
    class_id: int
    class_code: str
    created_at: Optional[datetime] = None

    class Config:
        from_attributes = True


class CourseAllListResponse(ResponseBase):
    """课程全量列表响应模型（管理员）"""
    data: List[CourseResponse]


class CourseInfo(BaseModel):
    """课程信息模型"""
    course_name: str
    teacher: TeacherInfo
    day_of_week: int
    location: Dict[str, Any]
    section: SectionInfo
    weeks: WeeksInfo
    courseType: Optional[str] = None


class ClassCoursesResponse(ResponseBase):
    """班级课程表响应模型"""
    data: Dict[str, Any]


class UserBase(BaseModel):
    """用户基础模型"""
    username: str


class UserCreate(UserBase):
    """用户创建模型"""
    password: str
    role: str = "admin"
    secret_key: str



class UserAuth(BaseModel):
    """用户认证请求模型"""
    username: str
    password: str


class UserInfo(BaseModel):
    """用户信息模型"""
    id: int
    username: str
    role: str


class TokenResponse(BaseModel):
    """令牌响应模型"""
    token: str
    expires_in: int
    user: UserInfo


class AuthResponse(ResponseBase):
    """认证响应模型"""
    data: TokenResponse


class ErrorResponse(BaseModel):
    """错误响应模型"""
    code: int
    message: str
    errors: Optional[Dict[str, List[str]]] = None
